package material

import (
	"cloud_android_backend/api/common"
	"cloud_android_backend/pkg/middleware/authorization"
	model "cloud_android_backend/pkg/models/users"
	"cloud_android_backend/pkg/modules/material"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/golang/glog"
	rb "modules.tenxcloud.com/common/middleware"
	"net/http"
)

func CategoryList(c *gin.Context) {
	li, exists := c.Get(authorization.LoginUserKey)
	if !exists {
		c.AbortWithStatusJSON(common.ErrorResponse("get user from content failed"))
		return
	}
	current := li.(*model.User)
	res, err := material.GetCategoriesList(current.UserID)
	if err != nil {
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse(res))
}

type CategoryCreateReq struct {
	Category string `json:"category"`
	Img      string `json:"img"`
}

func CategoryCreate(c *gin.Context) {
	li, exists := c.Get(authorization.LoginUserKey)
	if !exists {
		c.AbortWithStatusJSON(common.ErrorResponse("get user from content failed"))
		return
	}

	body, _ := c.Get(rb.RequestBodyKey)
	paymentForm := CategoryCreateReq{}
	if err := json.Unmarshal(body.([]byte), &paymentForm); err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}

	current := li.(*model.User)
	res, err := material.CreateCategory(current.UserID, paymentForm.Category, paymentForm.Img)
	if err != nil {
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse(res))
}
func MaterialsList(c *gin.Context) {

}

//func UploadVideos(c *gin.Context) {
//	li, exists := c.Get(authorization.LoginUserKey)
//	if !exists {
//		c.AbortWithStatusJSON(common.ErrorResponse("get user from content failed"))
//		return
//	}
//	current := li.(*model.User)
//	file, err := c.FormFile("file")
//
//	if err != nil {
//		fmt.Println("获取文件失败: %v", err)
//		//c.String(http.StatusBadRequest, "获取文件失败: %v", err)
//		c.JSON(common.ErrorResponse("get file failed"))
//		return
//	}
//	catId := c.PostForm("catId")
//	if catId == "" {
//		c.JSON(common.ErrorResponse("catId is empty"))
//		return
//	}
//	catIdInt, _ := strconv.Atoi(catId)
//	// 创建保存目录
//	saveDir := "/fsdata/material/" + catId
//	if err := os.MkdirAll(saveDir, os.ModePerm); err != nil {
//		//c.String(http.StatusInternalServerError, "创建目录失败: %v", err)
//		c.JSON(common.ErrorResponse("create directory failed"))
//		return
//	}
//
//	// 保存文件
//	dst := filepath.Join(saveDir, file.Filename)
//	if err := c.SaveUploadedFile(file, dst); err != nil {
//		//c.String(http.StatusInternalServerError, "保存文件失败: %v", err)
//		c.JSON(common.ErrorResponse("save file failed"))
//		return
//	}
//
//	c.JSON(success.OK(common.SuccessResponse("上传成功")))
//}
